package com.gql.constructorRef;

import com.gql.methodReferences.Employee;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * description:
 * @author: gql
 * @date: 2022/04
 */
public class ConstructorRef01Test {

    /**
     * 构造器引用
     */
    @Test
    public void constructorRefTest() {
        // 1.传统写法
        Supplier<Employee> sup = new Supplier<Employee>() {
            @Override
            public Employee get() {
                return new Employee();
            }
        };
        System.out.println(sup.get());
        //**************************************************
        // 2.lambda写法
        Supplier<Employee> sup1 = () -> new Employee();
        System.out.println(sup1.get());
        //**************************************************
        // 3.构造器引用
        Supplier<Employee> sup2 = Employee::new;
        System.out.println(sup2.get());
    }

    /**
     * 数组引用
     */
    @Test
    public void arrayRef02Test(){
        // lambda写法
        Function<Integer,String[]> func1 = length -> new String[length];
        String[] arr1 = func1.apply(5);
        System.out.println(Arrays.toString(arr1));
        //**************************************************
        // 数组引用
        Function<Integer,String[]> func2 = String[]::new;
        String[] arr2 = func2.apply(10);
        System.out.println(Arrays.toString(arr2));
    }
}
